package com.atstudy.servlet;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.atstudy.entites.Account;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2023/2/2 15:34
 * @copyright: 博为峰
 */
public class Test {
    public static void main(String[] args) {
//        m1();
        Account account1 = new Account(1, "zhangsan", 1000, new Date(), new Date());
        Account account2 = new Account(2, "lisi", 2000, new Date(), new Date());
        ArrayList<Account> list = new ArrayList<>();
        list.add(account1);
        list.add(account2);

        String jsonString = JSONArray.toJSONString(list);
//        [{"aid":1,"bwf":"com.atstudy","createTime":"2023-02-02","name":"zhangsan","updateTime":"2023-02-02 16:04:43.803"},
//        {"aid":2,"bwf":"com.atstudy","createTime":"2023-02-02","name":"lisi","updateTime":"2023-02-02 16:04:43.803"}]
        System.out.println(jsonString);

        JSONArray jsonArray = JSONArray.parseArray(jsonString);
        System.out.println("jsonArray = " + jsonArray);
    }

    private static void m1() {
        Account account = new Account(1, "zhangsan", 1000, new Date(), new Date());
        //将Java对象序列化为JSON
        String jsonString = JSON.toJSONString(account);
//        {"balance":1000,"bwf":"com.atstudy","createTime":"2023-02-02 15:51:09.044","id":1,"name":"zhangsan","updateTime":"2023-02-02 15:51:09.044"}
//        {"aid":1,"bwf":"com.atstudy","createTime":"2023-02-02","name":"zhangsan","updateTime":"2023-02-02 15:59:40.949"}
        System.out.println(jsonString);

        //将JSON解析为Java对象
        account = JSON.parseObject(jsonString, Account.class);
        System.out.println(account);
    }
}
